package com.rnkrsoft.suyan.config.server.ajax.authority.local;

import com.rnkrsoft.suyan.config.server.ajax.authority.AuthorityAuthenticator;
import com.rnkrsoft.suyan.config.server.ajax.authority.AuthorityContext;
import com.rnkrsoft.suyan.config.server.ajax.authority.Menu;
import com.rnkrsoft.suyan.config.server.system.dao.*;
import com.rnkrsoft.suyan.config.server.system.entity.OperateEntity;
import com.rnkrsoft.suyan.config.server.system.entity.UserEntity;
import com.rnkrsoft.suyan.config.server.system.entity.UserRoleEntity;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class LocalAuthorityAuthenticator implements AuthorityAuthenticator {
    @Autowired
    ActionDAO actionDAO;

    @Autowired
    UserDAO userDAO;

    @Autowired
    UserRoleDAO userRoleDAO;

    @Autowired
    RoleAuthorityDAO roleAuthorityDAO;

    @Autowired
    AuthorityDAO authorityDAO;


    @Override
    public boolean hasAuthority(AuthorityContext authorityContext) {
        UserEntity userEntity = userDAO.selectByPrimaryKey(authorityContext.getUserId());
        if (userEntity == null) {
            return false;
        }
        int authorityCount = authorityDAO.selectOperateCountByUserAndActionAndOperate(userEntity.getUserId(), authorityContext.getAction(), authorityContext.getOperate());
        return authorityCount > 0;
    }

    @Override
    public List<Menu> menus(AuthorityContext authorityContext) {

        return null;
    }
}
